﻿'Imports OMANE.Modules.PMT
Imports OMANE.Common.UI
Imports System.IO
Imports Microsoft.Practices.Prism.Commands

Public Enum ArboPMT_Chemin
    RACINEPMT
    RACINEINFOCENTRE
    FLUXHISTOCRT
    FLUXHISTOPMT
    FLUXTXCOM
    FLUXDERCBP
    FLUXREASS
End Enum

Public Class ViewModelNouveauPMTArbo
    Inherits ViewModelBase
    'Evolution :
    'Faire un dictionnaires
    'Le mot clé de la table / Sous forme de FileDictionary
    'Chemin vers un XML

    Public Sub New()
        InitFromDefaultArbo()
        _Mois = "201307"
    End Sub

    Public Sub New(Mois As String)
        InitFromDefaultArbo()
        'Retraiter chacun des noms en fonction de la valeur de MoisAnnee
    End Sub

    Private Sub InitFromDefaultArbo()
        'Charger le XML
        Dim DefaultArboconfig As XElement = XElement.Load( _
            IO.Path.Combine(IO.Path.GetDirectoryName(Reflection.Assembly.GetAssembly(Me.GetType()).Location), "DefaultArboConfig.xml"))

        'Créer l'objet arboPMT correspondant
        _RacineInfocentre = New DirectoryInfo(Decomplile_Chaine(DefaultArboconfig.<RacineInfocentre>.FirstOrDefault.Value))
        _RacineParametres = New DirectoryInfo(Decomplile_Chaine(DefaultArboconfig.<RacineParam>.FirstOrDefault.Value))

        _TbleFluxHistoCRT = New FichierPMT(Decomplile_Chaine(DefaultArboconfig.<TbleFluxHistoCRT>.FirstOrDefault.Value))
        _TbleFluxHistoPMT = New FichierPMT(Decomplile_Chaine(DefaultArboconfig.<TbleFluxHistoPMT>.FirstOrDefault.Value))
        _TbleFluxTxCom = New FichierPMT(Decomplile_Chaine(DefaultArboconfig.<TbleFluxTxCom>.FirstOrDefault.Value))
        _TbleFluxDerCBP = New FichierPMT(Decomplile_Chaine(DefaultArboconfig.<TbleFluxDerCBP>.FirstOrDefault.Value))
        _TbleFluxReass = New FichierPMT(Decomplile_Chaine(DefaultArboconfig.<TbleFluxReass>.FirstOrDefault.Value))
    End Sub


    Private _Mois As String
    Public Property Mois As String
        Get
            Return _Mois
        End Get
        Set(value As String)
            _Mois = value
        End Set
    End Property


#Region "Les répertoires du PMT"
    Private _RacineParametres As DirectoryInfo
    Public ReadOnly Property NomRacineParametres As String
        Get
            If _RacineParametres IsNot Nothing Then
                Return _RacineParametres.FullName
            Else
                Return ""
            End If
        End Get
    End Property
    Public ReadOnly Property ExistsRacineParametre As Boolean
        Get
            Return _RacineParametres.Exists
        End Get
    End Property

    Private _RacineInfocentre As DirectoryInfo
    Public ReadOnly Property NomRacineInfocentre As String
        Get
            If _RacineInfocentre IsNot Nothing Then
                Return _RacineInfocentre.FullName
            Else
                Return ""
            End If
        End Get
    End Property
    Public ReadOnly Property ExistsRacineInfocentre As Boolean
        Get
            Return _RacineInfocentre.Exists
        End Get
    End Property

    Private _TbleFluxHistoCRT As FichierPMT
    Public ReadOnly Property NomTbleFluxHistoCRT As String
        Get
            Return _TbleFluxHistoCRT.FileFullName
        End Get
    End Property
    Public ReadOnly Property ExistsTbleFluxHistoCRT As Boolean
        Get
            Return _TbleFluxHistoCRT.DirectoryExists
        End Get
    End Property

    Private _TbleFluxHistoPMT As FichierPMT
    Public ReadOnly Property NomTbleFluxHistoPMT As String
        Get
            Return _TbleFluxHistoPMT.FileFullName
        End Get
    End Property
    Public ReadOnly Property ExistsTbleFluxHistoPMT As Boolean
        Get
            Return _TbleFluxHistoPMT.DirectoryExists
        End Get
    End Property

    Private _TbleFluxTxCom As FichierPMT
    Public ReadOnly Property NomTbleFluxTxCom As String
        Get
            Return _TbleFluxTxCom.FileFullName
        End Get
    End Property
    Public ReadOnly Property ExistsTbleFluxTxCom As Boolean
        Get
            Return _TbleFluxTxCom.DirectoryExists
        End Get
    End Property

    Private _TbleFluxDerCBP As FichierPMT
    Public ReadOnly Property NomTbleFluxDerCBP As String
        Get
            Return _TbleFluxDerCBP.FileFullName
        End Get
    End Property
    Public ReadOnly Property ExistsTbleFluxDerCBP As Boolean
        Get
            Return _TbleFluxDerCBP.DirectoryExists
        End Get
    End Property

    Private _TbleFluxReass As FichierPMT
    Public ReadOnly Property TbleFluxReass As String
        Get
            Return _TbleFluxReass.FileFullName
        End Get
    End Property
    Public ReadOnly Property ExistsTbleFluxReass As Boolean
        Get
            Return _TbleFluxReass.DirectoryExists
        End Get
    End Property

    Public ReadOnly Property AllDirectoriesExist As Boolean
        Get
            Return (ExistsRacineInfocentre And ExistsRacineParametre And ExistsTbleFluxHistoCRT And ExistsTbleFluxHistoPMT And _
                    ExistsTbleFluxTxCom And ExistsTbleFluxDerCBP And ExistsTbleFluxReass)
        End Get
    End Property

#End Region

#Region "Les commandes"

    Private _CreerDossierCommand As New DelegateCommand(Of String)(AddressOf CreerDossierCanExecute, AddressOf CreerDossierExecute)
    Public ReadOnly Property OnCreerClick() As DelegateCommand(Of String)
        Get
            Return _CreerDossierCommand
        End Get
    End Property
    Private Function CreerDossierCanExecute(param As String) As Boolean
        Return True
    End Function
    Private Function CreerDossierExecute(param As String) As Boolean
        'MsgBox("Une demande de création de dossier a été faite")
        'Créer le dossier transmis en paramètre
        If Not param Is Nothing Then
            Dim DirectoryInfo As New DirectoryInfo(param)
            DirectoryInfo.Create()
            'Actualiser l'ensemble des propriétés
            _RacineParametres.Refresh()
            OnPropertyChanged("ExistsRacineParametre")

            _RacineInfocentre.Refresh()
            OnPropertyChanged("ExistsRacineInfocentre")

            _TbleFluxHistoCRT.Refresh()
            OnPropertyChanged("ExistsTbleFluxHistoCRT")

            _TbleFluxHistoPMT.Refresh()
            OnPropertyChanged("ExistsTbleFluxHistoPMT")

            _TbleFluxTxCom.Refresh()
            OnPropertyChanged("ExistsTbleFluxTxCom")

            _TbleFluxDerCBP.Refresh()
            OnPropertyChanged("ExistsTbleFluxDerCBP")

            _TbleFluxReass.Refresh()
            OnPropertyChanged("ExistsTbleFluxReass")

            OnPropertyChanged("AllDirectoriesExist")
        End If
        Return True
    End Function

    'Private _ValiderArboCommmand As New DelegateCommand(Of System.Windows.Window)(AddressOf ValiderArboCanExecute, AddressOf ValiderArboExecute)
    'Public ReadOnly Property OnValiderArboClic As DelegateCommand(Of System.Windows.Window)
    '    Get
    '        Return _ValiderArboCommmand
    '    End Get
    'End Property
    'Private Function ValiderArboCanExecute(window As System.Windows.Window) As Boolean
    '    Return True
    'End Function
    'Private Function ValiderArboExecute(window As System.Windows.Window) As Boolean
    '    window.Close()
    '    Return True
    'End Function

#End Region

    Private Function Decomplile_Chaine(chaine As String)
        Dim result As String = chaine
        result = result.Replace("%RacineInfocentre%", NomRacineInfocentre)
        result = result.Replace("%RacineParametres%", NomRacineParametres)
        result = result.Replace("%Mois%", Mois)
        Return result
    End Function

End Class